var express = require('express')
var serverStatic = require('serve-static')
//此中间件会把页面上post过来的值根据表单中的name属性自动封装成对象
var bodyParser = require('body-parser')
//nodejs自带的路径解析工具
var path = require('path')
//node连接mongoose的解析器
var mongoose = require('mongoose')
//cookie在express4.x中迁出，需要按照模块加载
var cookieParser = require('cookie-parser')
var cookieSession = require('cookie-session')
//替代之前版本的express.logger
var logger = require('morgan')
//引入路由模块
var route = require('./route/route.js')
//定义端口号
var port = 8090
//建立服务器连接,其中forum为数据库名
mongoose.connect('mongodb://localhost/forum')
//定义express框架
var app = express()
//设置视图的访问路径
app.set('views',path.join(__dirname,'views'))
//设置视图引擎模板为ejs
app.set('view engine','ejs')
//设置端口号
app.set('port',port)
//设置静态访问目录
app.use(serverStatic(path.join(__dirname,'public')))
app.use(bodyParser.urlencoded({extended:true}))

app.use(cookieParser())
app.use(cookieSession({
	secret: 'forum',
	resave: false,
	saveUninitialized:true
}))

//设置模式为development
if('development'  === app.get('env')){
	app.set('showStackError',true)
	app.use(logger(':method :url :status'))
	app.locals.pretty = true
	mongoose.set('debug',true)
}
//引入路由相关操作
route(app)
//设置监听的端口
app.listen(app.get('port'),function(err){
	if(err){
		console.log(err)
	}
	console.log('server start on '+app.get('port'))
})